package ControlLayer;

//import static org.junit.Assert.fail;
import DBLayer.DBOffer;
import ModelLayer.Customer;
import ModelLayer.Offer;
import java.util.ArrayList;

public class CtrOffer {
	/**
	 * creates an Offer
	 * @param calculatedPrice
	 * @param dateStart
	 * @param dateEnd
	 * @param acceptDeadline
	 * @param discount
	 * @param statusOffer
	 * @param sentDate
	 * @param pricePerHour 	 
	 */
    public int insertOffer(String idCustomer, double calculatedPrice, String dateStart, String dateEnd, String acceptDeadline, double discount, String statusOffer, String sentDate, double pricePerHour) throws Exception
    {
        Offer off = new Offer();
        int i = 0;

        off.setIdOffer("nextIdOffer");
        off.setCustomer(new Customer(idCustomer));
        off.setCalculatedPrice(calculatedPrice);
        off.setDateStart(dateStart);
        off.setDateEnd(dateEnd);
        off.setAcceptDeadline(acceptDeadline);
        off.setDiscount(discount);
        off.setStatusOffer(statusOffer);
        off.setSentDate(sentDate);
        off.setPricePerHour(pricePerHour);

        DBOffer dbOff = new DBOffer();
        i = dbOff.insertOffer(off);
        return i;
    }
	
	/**
	 * 
	 * @return an ArrayList containing all objects Offer in the database
	 * @throws Exception
	 */
	public ArrayList<Offer> GetAllOffers() throws Exception 
		{
	        ArrayList<Offer> offerList = new ArrayList<Offer>();
	        DBOffer dbOff = new DBOffer();
	        offerList = dbOff.getAllOffers();
	
	        return offerList;
        }
    
	/**
	 * 
	 * @param idOffer
	 * @return an object Offer with a given idOffer
	 * @throws Exception
	 */
	public Offer findOfferId(String idOffer) throws Exception 
		{
			DBOffer dbOff = new DBOffer();
			Offer offer = new Offer();
			offer = dbOff.findOfferId(idOffer);
			
			return offer;
		}
	
	/**
	 * 
	 * @param idOffer
	 * @param calculatedPrice
	 * @param dateStart
	 * @param dateEnd
	 * @param acceptDeadline
	 * @param discount
	 * @param statusOffer
	 * @param sentDate
	 * @param pricePerHour
	 * @return i and updates an object Offer with a given idOffer
	 * @throws Exception
	 */
	public int updateOffer(String idOffer, double calculatedPrice, String dateStart, String dateEnd, String acceptDeadline, double discount, String statusOffer, String sentDate, double pricePerHour) throws Exception 
		{
	      Offer offer = new Offer();
	      DBOffer dbOff = new DBOffer();
	      offer = dbOff.findOfferId(idOffer);
	      int i = 0;
	      
	      if( offer != null)
	      {
		      offer.setCalculatedPrice(calculatedPrice);
		      offer.setDateStart(dateStart);
		      offer.setDateEnd(dateEnd);
		      offer.setAcceptDeadline(acceptDeadline);
		      offer.setDiscount(discount);
		      offer.setStatusOffer(statusOffer);
		      offer.setSentDate(sentDate);
		      offer.setPricePerHour(pricePerHour);
		      i = dbOff.updateOffer(offer);
	      }
	      
	      return i;
		}
	
	/**
	 * 
	 * @param idOffer
	 * @return i and deletes an object Offer with a given idOffer
	 * @throws Exception
	 */
	public int deleteOffer(String idOffer) throws Exception 
	  	{
		  Offer off = new Offer();
		  DBOffer dbOff = new DBOffer();
		  off = dbOff.findOfferId(idOffer);
		  int i = 0;
		
		  if (off != null){
		      i = dbOff.deleteOffer(idOffer);
		
		  }
		  return i;
	  	}
}
